﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EZCRS.HCMS.Data;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdmCardNumb
    {
        private string SqlStr = "";

        #region 分配卡号
        /// <summary>
        /// 分配卡号
        /// </summary>
        /// <returns></returns>
        public bool CardNumb_Distribute(string Gid, string Uid, string Cardid, string Useadm, string Comment, out SqlCommand Outcmd)
        {
            int IdOut = 0;
            SqlParameter[] pam = new SqlParameter[6];
            pam[0] = new SqlParameter("@gid", Gid);
            pam[1] = new SqlParameter("@uid", Uid);
            pam[2] = new SqlParameter("@cardid", Cardid);
            pam[3] = new SqlParameter("@useadm", Useadm);
            pam[4] = new SqlParameter("@comment", Comment);
            pam[5] = new SqlParameter("@idOut", IdOut);
            pam[5].Direction = ParameterDirection.Output;

            SqlStr = "hfg_cardnumberdistribute";    //存储过程名字
            if (ExecSql.SqlExecNoqueryByProcedure(SqlStr, out Outcmd, pam))
                return true;
            else
                return false;
        }
        #endregion

        #region 常客会员卡的批量删除
        public bool CardNumb_Del(string Cardid)
        {
            string sql = "";
            sql = "delete from hfg_cardnumber where cardid in (" + Cardid + ")";
            if (ExecSql.SqlExecNoquery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 常客会员卡列表
        public DataTable CardNumb_List(string StrWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from hfg_cardnumber where 1=1 ");
            if (StrWhere.Trim() != "")
                strSql.Append(StrWhere);
            return ExecSql.GetDataSet(strSql.ToString()).Tables[0];
        }

        public DataSet CardNumb_List1(int cpage, string StrWhere)
        {
            DataSet ds = ExecSql.AddParamFrKeyField("hfg_cardnumber", "*", " cardid desc", "cardid", CFun.PageSize, cpage, StrWhere);
            return ds;
        }

        public DataTable CardNumb_List(string StrWhere, string SType)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select cardnumber,cardpassword,cardtype from hfg_cardnumber where 1=1 ");
            if (StrWhere.Trim() != "")
                strSql.Append(StrWhere);
            return ExecSql.GetDataSet(strSql.ToString()).Tables[0];
        }
        #endregion

        #region 常客会员卡信息批量添加
        /// <summary>
        /// 批量添加常客会员卡信息
        /// </summary>
        /// <returns></returns>
        public bool CardNumb_Add(string Orgid, string Orgtype, string Cardtype, string Templateno, string Startnum, string Endnum, string Median, out SqlCommand Outcmd)
        {
            int IdOut = 0;
            Orgtype = "0";
            SqlParameter[] pam = new SqlParameter[8];
            pam[0] = new SqlParameter("@orgid", Orgid);
            pam[1] = new SqlParameter("@orgtype", Orgtype);
            pam[2] = new SqlParameter("@cardtype", Cardtype);
            pam[3] = new SqlParameter("@templateno", Templateno);
            pam[4] = new SqlParameter("@startnum", Startnum);
            pam[5] = new SqlParameter("@endnum", Endnum);
            pam[6] = new SqlParameter("@median", Median);
            pam[7] = new SqlParameter("@idOut", IdOut);
            pam[7].Direction = ParameterDirection.Output;

            SqlStr = "hfg_batchcardnumberinsert";    //存储过程名字
            if (ExecSql.SqlExecNoqueryByProcedure(SqlStr, out Outcmd, pam))
                return true;
            else
                return false;
        }
        #endregion

        #region 常客会员卡信息添加
        /// <summary>
        /// 添加常客会员卡信息
        /// </summary>
        /// <returns></returns>
        public bool CardNumb_Add(string Orgid, string Orgtype, string Cardnumber, string Cardpassword, string Cardtype)
        {
            SqlParameter[] pam = new SqlParameter[5];
            pam[0] = new SqlParameter("@orgid", Orgid);
            pam[1] = new SqlParameter("@orgtype", Orgtype);
            pam[2] = new SqlParameter("@cardnumber", Cardnumber);
            pam[3] = new SqlParameter("@cardpassword", Cardpassword);
            pam[4] = new SqlParameter("@cardtype", Cardtype);


            SqlStr = "insert into hfg_cardnumber(orgid,orgtype,cardnumber,cardpassword,cardtype) values(@orgid,@orgtype,@cardnumber,@cardpassword,@cardtype)";

            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 批量导入
        /// <summary>
        /// 批量导入，返回 成功、失败数
        /// </summary>
        /// <param name="Orgid">组织编号</param>
        /// <param name="Orgtype">组织类型</param>
        /// <param name="cardtxt">卡号列</param>
        /// <param name="Cardtype">卡类型</param>
        /// <returns></returns>
        public int[] CardNumb_Add(string Orgid, string Orgtype, string cardtxt, string Cardtype)
        {
            int[] i = new int[2];
            i[0] = 0;
            i[1] = 0;

            string[] CardItemList = cardtxt.Split('\n');
            if (CardItemList.Length > 1)
            {
                //有数据
                for (int j = 1; j < CardItemList.Length; j++)
                {
                    string CardItem = CardItemList[j];
                    string[] cardCol = CardItem.Split('\t');
                    if (cardCol.Length > 2)
                        if (CardNumb_Add(Orgid, Orgtype, cardCol[0], cardCol[1], cardCol[2]))
                            i[0]++;
                        else
                            i[1]++;
                    else
                        i[1]++;
                }
            }
            return i;
        }
        #endregion
    }
}
